<?php
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
$ui = new UI();
include( "admin_menu.php" );
$rs = getdb( "select * from lotto where open < '".date( "Y-m-d" )."' order by open desc" );
if ( $rs->eof )
{
    $ui->showErrorPage( "没有资料！" );
    exit();
}
$id = $rs->f['id'];
$open = $rs->f['open'];
$round = $rs->f['round'];
echo "<p>计算 第 {$round} 期 {$open} 的限额</p>";
$conf = new Config();
if ( isset( $_POST['ok'] ) && $_POST['ok'] == "1" )
{
    $sql = "select id,amount,maxamountp,oneamount from k_user where acl=1 and enabled=1";
    $rs = getdb( $sql );
    while ( !$rs->eof )
    {
        $rs1 = getdb( "select sum(amount) from bet where user_id=".$rs->f['id']." and lotto_id=".$id." and rame=9" );
        $maxamount = intval( $rs1->f[0] );
        $maxamount = $maxamount * $rs->f['maxamountp'];
        $oneamount = $rs->f['oneamount'];
        $maxamount = ceil( $maxamount / 1000 ) * 1000;
        if ( $rs->f['maxamountp'] == 100 )
        {
            $maxamount = $rs->f['amount'];
        }
        else
        {
            if ( 0 < $maxamount && $maxamount < $oneamount )
            {
                $maxamount = $oneamount;
            }
            if ( $rs->f['amount'] <= $maxamount )
            {
                $maxamount = $rs->f['amount'];
            }
        }
        getdb( "update k_user set maxamount=".$maxamount." where id=".$rs->f['id'] );
        $rs->movenext();
    }
    echo "<p><font color=red><b>计算完毕！</b></font></p>";
    echo "</table>";
}
else
{
    $rate = floatval( $conf->get( "maxamount_rate" ) );
    $min = intval( $conf->get( "maxamount_min" ) );
    if ( $rate == 0 )
    {
        $rate = 3;
    }
    echo "<form action=op.php?op=admin&fp=maxamount method=post>";
    echo "<input type=hidden name=ok value=1>";
    echo "<br>";
    echo "<input type=submit value=开始计算>";
    echo "</form>";
}
echo "</table>";
$ui->footer();
?>